<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;    //引入hash
class NobilityAuthRepository
{
    /**
     * Notes:获取爵位权限列表
     * User: wangyueli
     *
     * @return \App\Models\MainDB\NobilityAuth|\Laravel\Lumen\Application|mixed
     */
    public function getNobilityAuthList()
    {
        $list = app('model_nobility_auth');
        $list = $list->where('deleted', 0)->orderBy('sort')->orderBy('id','desc')->get();

        return $list;
    }

    /**
     * Notes:新增 爵位权限
     * User: wangyueli
     *
     * @param  array  $params
     * @return mixed
     * @throws BasicException
     */
    public function nobilityAuthAdd($params)
    {
        $params['add_time'] = date('Y-m-d H:i:s');
        $params['update_time'] = date('Y-m-d H:i:s');

        //新增爵位权限
        $result = app('model_nobility_auth')->create($params);
        if ($result) {
            return $result;
        }
        throw new BasicException(93013,'爵位权限添加失败');
    }

    /**
     * Notes:编辑 爵位权限
     * User: wangyueli
     *
     * @param  array  $params
     * @return bool
     * @throws BasicException
     */
    public function nobilityAuthEdit($params)
    {
        //判断数据是否存在
        $nobilityDetail = app('model_nobility_auth')->where('id', $params['id'])->where('deleted',0)->first();
        if (empty($nobilityDetail)) {
            throw new BasicException(50002,'爵位权限不存在');
        }

        $rankSave = app('model_nobility_auth')->where('id', $params['id'])->update($params);
        if ($rankSave !== false) {
            return true;
        }
        throw new BasicException(93014,'爵位权限编辑失败');
    }

    /**
     * Notes:删除和批量删除 爵位权限
     * User: wangyueli
     *
     * @param  array  $params
     * @return mixed
     * @throws BasicException
     */
    public function nobilityAuthDel($params)
    {
        $rankDel = app('model_nobility_auth')->whereIn('id', $params)->update(['deleted'=>1]);
        if ($rankDel) {
            return $rankDel;
        }
        throw new BasicException(93015,'爵位权限删除失败');
    }

    /**
     * Notes:获取权限列表下拉菜单
     * User: wangyueli
     *
     * @return \App\Models\MainDB\NobilityAuth|\Laravel\Lumen\Application|mixed
     */
    public function getAuthSelect()
    {
        $list = app('model_nobility_auth');
        $list = $list->where('deleted', 0)
            ->where('status',1)
            ->orderBy('sort')
            ->orderBy('id','desc')
            ->get(['id','name']);

        return $list;
    }
}


?>